/**
 * This class represents an rss feed. It stores each item of the rss feed
 * as an RssItem object. 
 * @author Chirag Mistry
 */

package ca.uwo.android.rss;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.util.Log;

public class RssFeed implements Serializable{
	
	private static final long serialVersionUID = 4471036434448410335L;

	private List<RssItem> rssFeed; // the rss feed
	private int maxEvents; // the maximum number of events

	// that occur in any date in the feed

	/**
	 * Constructor for the rss feed
	 */
	public RssFeed() {
		rssFeed = new ArrayList<RssItem>();
	}

	/**
	 * This method adds an rss item to the rss feed
	 * 
	 * @param rssItem
	 *            the item to be added to the feed
	 * @return boolean indicates if the item was added or not
	 */
	public boolean add(RssItem rssItem) {
		return rssFeed.add(rssItem);
	}

	public boolean remove(RssItem rssItem) {
		return rssFeed.remove(rssItem);
	}

	public RssItem remove(int index) {
		return rssFeed.remove(index);
	}

	/**
	 * This method gets an rss item from the rss feed
	 * 
	 * @param location
	 *            index of the item in the list of rss items
	 * @return RssItem the item at the specified index
	 */
	public RssItem get(int location){
		
		return rssFeed.get(location);
		
	}

	/**
	 * This method returns the number of items in the rss feed
	 * 
	 * @return int size of the rss feed
	 */
	public int size() {
		return rssFeed.size();
	}

	/**
	 * This method returns a list of unique dates in the rss feed
	 * 
	 * @return dates array of unique dates
	 */
	public String[] getDates() {

		Vector<String> dates = new Vector<String>();
		int eventTotal = 1;
		maxEvents = 0;
		String thisDate = null;

		for (RssItem item : rssFeed) {

			thisDate = item.getDate();

			if (dates.size() == 0 || !dates.lastElement().equals(thisDate)) {
				eventTotal = 0;
				dates.add(thisDate);
			}

			eventTotal++;
			if (eventTotal > maxEvents) {
				maxEvents = eventTotal;
			}

		}
		
		return dates.toArray(new String[dates.size()]);
	}

	public int getMaxEvents() {
		return maxEvents;
	}
	
	public List<RssItem> getItems(){
		return rssFeed;
	}
}
